package whf.framework.tools.generator;

import whf.framework.exception.CreateException;
import whf.framework.exception.DAOException;
import whf.framework.exception.NotFoundException;
import whf.framework.exception.RemoveException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.exception.UpdateException;
import whf.framework.meta.MetaFactory;
import whf.framework.service.ServiceImp;
import whf.framework.util.BeanFactory;
import whf.framework.util.Output;

/**
 * @author wanghaifeng
 *
 */
public class ServiceImpGenerator extends AbstractGenerator{
	private String packageName;

	private String serviceImpClassName;

	private String serviceClassName;

	private String boClassName;
	
	private String daoClassName;

	public ServiceImpGenerator(String packageName, String serviceImpClassName, String serviceClassName,
			String boClassName, String daoClassName) {
		this.packageName = packageName;
		this.serviceImpClassName = serviceImpClassName;
		this.boClassName = boClassName;
		this.serviceClassName = serviceClassName;
		this.daoClassName = daoClassName;
	}

	protected void gen() {
		super.setPackageName(this.packageName+".service");
		super.addImport(this.packageName+".entity.*");
		super.addImport(this.packageName+".dao.*");
		super.addImport(DAOException.class.getPackage().getName()+".*");
		super.addImport(BeanFactory.class.getName());
		super.addImport(MetaFactory.class.getName());
		super.setClassName(this.serviceImpClassName);
		super.setSuperClassName(ServiceImp.class.getName());
		super.setInterfaces(new String[]{this.serviceClassName});
		String boParamName = boClassName.substring(0,1).toLowerCase() + boClassName.substring(1);
		String daoParamName = daoClassName.substring(0,1).toLowerCase()+daoClassName.substring(1);
		super.addProp(this.daoClassName,daoParamName,true,false, "dao name");
		super.addMethod("public static","get"+this.serviceClassName,this.serviceClassName,null,null,new Class[]{ServiceNotFoundException.class},
				"return ("+this.serviceClassName+")BeanFactory.getService(MetaFactory.findByServiceClass("+this.serviceImpClassName+".class))");
		daoParamName = "this."+daoParamName;
		super.addRealMethod("create","void",new String[]{boClassName},new String[]{boParamName},
				new Class[]{CreateException.class},daoParamName+".create(" + boParamName + ")");
		super.addRealMethod("update","void",new String[]{boClassName},new String[]{boParamName},
				new Class[]{UpdateException.class},daoParamName+".update(" + boParamName + ")");
		super.addRealMethod("remove","void",new String[]{boClassName},new String[]{boParamName},
				new Class[]{RemoveException.class},daoParamName+".remove(" + boParamName + ")");
		super.addRealMethod("remove","void",new String[]{"long"},new String[]{"id"},
				new Class[]{RemoveException.class},daoParamName+".remove(id)");
		StringBuffer sb = new StringBuffer();
		sb.append("for(int i=0;ids!=null && i< ids.length;i++){").append(LINE_SEPARATOR);
		sb.append("\t\t"+daoParamName+".remove(ids[i]);").append(LINE_SEPARATOR);
		sb.append("\t}");
		super.addRealMethod("remove","void",new String[]{"long[]"},new String[]{"ids"},
				new Class[]{RemoveException.class},sb.toString());
		super.addRealMethod("findByPrimaryKey",boClassName,new String[]{"long"},new String[]{"id"},
				new Class[]{NotFoundException.class},"return ("+boClassName+")"+daoParamName+".findByPrimaryKey(id)");
	}

	public static void main(String args[]) throws Exception {
		Output out = new Output(System.out);
		ServiceImpGenerator gen = new ServiceImpGenerator("com.hh.shop", "ShopDAO",
				"IShopService", "ShopDO","IShopDAO");
		gen.output(out);
	}

}
